package main

import (
	"fmt"
	"log"

	"github.com/go-ole/go-ole"
	"github.com/go-ole/go-ole/oleutil"
)

func main() {
	addWithClsID()
	addWithProgID()
}

func addWithProgID() {
	ole.CoInitialize(0)
	defer ole.CoUninitialize()

	obj, err := oleutil.CreateObject("Sample.Adder") // ProgID 方式
	if err != nil {
		log.Fatal(err)
	}

	disp, err := obj.QueryInterface(ole.IID_IDispatch)
	if err != nil {
		log.Fatal(err)
	}
	defer disp.Release()

	ret, _ := oleutil.CallMethod(disp, "Add", 2, 5)
	log.Println("2+5=", ret.Value())

}

func addWithClsID() {
	ole.CoInitialize(0)
	defer ole.CoUninitialize()

	clsid, err := ole.CLSIDFromString("{d8de1d74-0296-4462-8825-99e459b9479b}")
	if err != nil {
		fmt.Println(err)
		return
	}

	unknown, err := ole.CreateInstance(clsid, nil)
	if err != nil {
		fmt.Println(err)
		return
	}
	defer unknown.Release()

	disp, err := unknown.QueryInterface(ole.IID_IDispatch)
	if err != nil {
		log.Fatalf("获取 IDispatch 失败: %v", err)
	}
	defer disp.Release()

	// 调用 Add 方法
	ret, _ := oleutil.CallMethod(disp, "Add", 10, 20)
	log.Println("10+20=", ret.Value())

}
